package com.cdd.agent.controller;

import cn.hutool.core.bean.BeanUtil;
import com.cdd.common.constant.RedisKeys;
import com.cdd.common.core.controller.BaseController;
import com.cdd.common.core.domain.Result;
import com.cdd.common.core.page.TableDataInfo;
import com.cdd.common.core.redis.RedisCache;
import com.cdd.common.domain.entity.AppUser;
import com.cdd.agent.api.domain.dto.AgentCashWithdrawaltDto;
import com.cdd.agent.api.domain.dto.AgentCountDto;
import com.cdd.agent.api.domain.dto.GuildRatioSetupDto;
import com.cdd.agent.api.domain.dto.MingxiChongzhiDto;
import com.cdd.agent.api.domain.vo.*;
import com.cdd.agent.api.service.AgentUserCountService;
import com.cdd.agent.api.service.AppUserService;
import com.cdd.agent.api.service.GuildUserCountService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/guild")
public class GuildCountController extends BaseController {

    @Resource
    private GuildUserCountService guildUserCountService;

    @Resource
    private AgentUserCountService agentUserCountService;

    @Resource
    private AppUserService appUserService;

    @Resource
    private RedisCache redisUtil;

    /**
     * @desc 数据统计
     */
    @GetMapping("/count")
    public Result count() {
        return sucessJSON(guildUserCountService.getCount(getUserId()));
    }

    /**
     * @desc 工会主播自身统计
     */
    @GetMapping("/count1")
    public TableDataInfo count1(AgentCountDto dto) {
        dto.setUserId(getUserId());
        startPage();
        List<GuildUserOwnerCountVo> list = guildUserCountService.getGuildUserCount1(dto);
        return getDataTable(list);
    }

    /**
     * @desc 工会贡献统计
     */
    @GetMapping("/count2")
    public TableDataInfo count2(AgentCountDto dto) {
        dto.setUserId(getUserId());
        startPage();
        List<GuildDevoteCountVo> list = guildUserCountService.getGuildUserCount2(dto);
        return getDataTable(list);
    }

    /**
     * @desc 主播提现记录
     */
    @GetMapping("/tx/list")
    public TableDataInfo getTxList(AgentCashWithdrawaltDto dto) {
        dto.setUserId(getUserId());
        startPage();
        List<CashWithdrawalVo> list = guildUserCountService.getTxList(dto);
        return getDataTable(list);
    }

    /**
     * @desc 充值记录
     */
    @GetMapping("/cz/list")
    public TableDataInfo getCzList(MingxiChongzhiDto dto) {
        AppUser user = getAppUser();
        if (user == null){
            return getDataTable(Collections.emptyList());
        }
        UserBaseDto baseDto = BeanUtil.copyProperties(user, UserBaseDto.class);
        dto.setUserId(getUserId());
        List<MingxiChongzhiVo> list;
        startPage();
        if (baseDto.getLoginType() == 1) {//代理
            list = agentUserCountService.getAgentUserCZList(dto);
        } else {
            list = guildUserCountService.getGuildUserCZList(dto);
        }
        return getDataTable(list);
    }

    /**
     * @desc 收益汇总
     */
    @GetMapping("/count3")
    public Result count3() {
        return sucessJSON(guildUserCountService.getGuildIncomeCount(getUserId()));
    }

    /**
     * @desc 工会提成比例
     */
    @GetMapping("/ratio")
    public Result ratio() {
        return sucessJSON(guildUserCountService.getProfitRatio(getUserId()));
    }

    /**
     * @desc 工会提成比例设置
     */
    @PostMapping("/ratio/setup")
    public Result ratioSetup(@RequestBody GuildRatioSetupDto setupDto) {
        if (setupDto.getProfitRatio() == null) {
            return errorJSON("请填写提成比例");
        }
        setupDto.setUserId(getUserId());
        guildUserCountService.saveGuildRatio(setupDto);
        return sucessJSON("设置成功");
    }

    /**
     * 主播账号解禁
     *
     * @param userId
     * @return
     */
    @RequestMapping("/zbjj")
    public Result jiejin(Integer userId) {
        if (guildUserCountService.validNotGuildUser(getUserId(), userId)) {
            return this.errorJSON("当前用户不能操作此主播");
        }
        if (userId == null) {
            return this.errorJSON("请选择要禁用的账号");
        }
        return jinyong(userId, 1, 0);
    }

    public Result jinyong(Integer id, Integer state, Integer hour) {
        if (id == null) {
            return this.errorJSON("未获取到相关用户信息");
        }
        if (state == null) {
            return this.errorJSON("状态不能为空");
        }
        appUserService.upstate(id, state, hour);
        String key = String.format(RedisKeys.USER, id);
        redisUtil.deleteObject(key);
        return this.sucessJSON("保存成功");
    }

    /**
     * 主播账号禁用
     *
     * @param userId
     * @return
     */
    @RequestMapping("/zbjy")
    public Result jinyong(Integer userId, Integer hour) {
        if (guildUserCountService.validNotGuildUser(getUserId(), userId)) {
            return this.errorJSON("当前用户不能操作此主播");
        }
        if (userId == null) {
            return this.errorJSON("请选择要禁用的账号");
        }
        return jinyong(userId, 1, 0);
    }

}
